home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio_setvbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  1.4 KB  |  70 lines

  1. /*            s e t v b u f
  2.  *
  3.  * Set the type of buffering to be used on this stream. This
  4.  * must be called before any read or write has been done on
  5.  * the stream. It is legal to make a stream unbuffered and
  6.  * then at some subsequent time, make it buffered.
  7.  *
  8.  * Input streams cannot be line buffered. Attempts to do so
  9.  * will make them fully buffered instead.
  10.  *
  11.  * Patchlevel 1.0
  12.  *
  13.  * Edit History:
  14.  */
  15.  
  16. #include "stdiolib.h"
  17.  
  18. /*LINTLIBRARY*/
  19.  
  20. int setvbuf(fp, buf, type, size)
  21.  
  22. FILE *fp;                /* stream */
  23. char *buf;                /* buffer */
  24. int type;                /* type of buffering */
  25. unsigned size;                /* size of buffer */
  26.  
  27. {
  28. #ifdef __STDC__
  29.   void *malloc(unsigned int);            /* memory allocator */
  30. #else
  31.   void *malloc();            /* memory allocator */
  32. #endif
  33.  
  34.   if (TESTFLAG(fp, _IONBF))
  35.     fp->_base = NULL;
  36.  
  37.   if (fp->_base != NULL)
  38.     return EOF;
  39.  
  40.   CLEARFLAG(fp, (_IOFBF | _IONBF | _IOLBF));
  41.  
  42.   if (type == _IOFBF || type == _IOLBF) {
  43.     if (size == 0)
  44.       return EOF;
  45.  
  46.     if (buf == NULL) {
  47.       if ((buf = (char *) malloc(size)) == NULL)
  48.         return EOF;
  49.       else
  50.     SETFLAG(fp, _IOMYBUF);
  51.     }
  52.  
  53.     fp->_base   = (unsigned char *) buf;
  54.     fp->_bufsiz = size;
  55.  
  56.     if (TESTFLAG(fp, _IOREAD))
  57.       type = _IOFBF;
  58.   }
  59.   else if (type == _IONBF) {
  60.     fp->_base = &fp->_buf;
  61.     fp->_bufsiz = sizeof(fp->_buf);
  62.   }
  63.   else
  64.     return EOF;
  65.  
  66.   SETFLAG(fp, type);
  67.   INITBUFFER(fp);
  68.   return 0;
  69. }
  70.